package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    /**
     * 员工登录
     * @param request
     * @param employee
     * @return
     */
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request,@RequestBody Employee employee){
        log.info("员工信息：{}",employee);

        //先对接受到的密码，进行加密处理
        String password = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());


        //构造条件构造器
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername,employee.getUsername());
        Employee employee1 = employeeService.getOne(queryWrapper);

        //判断用户名在数据库是否存在
        if(employee1 == null){
            return R.error("用户名不存在");
        }

        //如果用户存在，判断密码是否正确
        if(!employee1.getPassword().equals(password)){
            return R.error("登录失败，密码错误");
        }

        //判断账号当前的状态是否禁用
        if(employee1.getStatus() == 0){
            return R.error("登录失败，账号已禁用");
        }

        //把用户名存在session中
        request.getSession().setAttribute("employee",employee1.getId());

        return R.success(employee1);
    }

    /**
     * 员工退出
     * @return
     */
    @PostMapping("/logout")
    public R<String> logout(HttpSession session){
        //直接把session中的id从session中移除就可以了
        session.removeAttribute("employee");
        return R.success("退出成功");
    }

    /**
     * 员工信息分页
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){
        log.info("分页数据信息page:{},pageSize:{},name:{}",page,pageSize,name);

        //构造分页构造器
        Page<Employee> employeePage = new Page<>();

        //构造条件构造器
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(name),Employee::getName,name);
        queryWrapper.orderByDesc(Employee::getUpdateTime);

        employeeService.page(employeePage,queryWrapper);
        return R.success(employeePage);
    }

    /**
     * 新增员工
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody Employee employee){
        log.info("新增员工信息：{}",employee);

        //设置员工的初始密码
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        employeeService.save(employee);

        return R.success("添加成功");
    }
}
